package com.singerw.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.singerw.mapper.TwUserRoleMapper;
import com.singerw.pojo.TwRole;
import com.singerw.pojo.TwUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0
 * @Package com.singerw.service
 * @auhter 张欣
 * @date 2020/12/30-4:12 PM
 */

@Service
public class TwUserRoleService {

    @Resource
    private TwUserRoleMapper twUserRoleMapper;

    @Autowired
    private TwRoleService twRoleService;

    /**
     * 通过tw_user_role 去检索当前用户所拥有的角色集。
     * @param userId
     * @return 用户所拥有的角色集
     */
    public ArrayList<TwRole> findRoleByUserId(String userId){
        QueryWrapper<TwUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<TwUserRole> twUserRoles = twUserRoleMapper.selectList(wrapper);
        ArrayList<TwRole> twRoles = new ArrayList<>();
        twUserRoles.forEach(item -> {
            twRoles.add(twRoleService.findById(item.getRoleId()));
        });
        return twRoles;
    }

    public void save(TwUserRole twUserRole){
        if(twUserRole.getId() == 0){
            twUserRoleMapper.insert(twUserRole);
            return;
        }
        twUserRoleMapper.updateById(twUserRole);
    }

}
